package com.put.mybills.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.put.mybills.model.Bill;
import com.put.mybills.model.Unit;

public class UnitDataSource {
	
	  private static String TAG = "UnitDataSource";
	  private SQLiteDatabase database;
	  private DatabaseHelper dbHelper;	  

	  private String[] allColumns = { 	DatabaseHelper.UNIT_ID,
			  							DatabaseHelper.UNIT_FACTURE_ID,
			  							DatabaseHelper.UNIT_BILL_ID,
			  							DatabaseHelper.UNIT_NAME,
			  							DatabaseHelper.UNIT_METRIC,
			  							DatabaseHelper.UNIT_PRICE,
			  							DatabaseHelper.UNIT_VALUE};
			  							

	  public UnitDataSource(Context context) {
	    dbHelper = new DatabaseHelper(context);
	  }
	
	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }
	
	  public void close() {
	    dbHelper.close();
	  }
	  
	  
	  public void createUnit(Unit unit) {  
		  	
		  	Log.d(TAG, "Create bill function");
			ContentValues values = new ContentValues();
			values.put(DatabaseHelper.UNIT_NAME, unit.getName());
			values.put(DatabaseHelper.UNIT_METRIC, unit.getMetric());	
			values.put(DatabaseHelper.UNIT_PRICE, unit.getPrice());	
			values.put(DatabaseHelper.UNIT_FACTURE_ID, unit.getFactureID());
			values.put(DatabaseHelper.UNIT_BILL_ID, unit.getBillID());
			values.put(DatabaseHelper.UNIT_VALUE, unit.getValue());
		
			
			long insertId = database.insert(DatabaseHelper.UNIT_TABLE_NAME, null, values);
			
			Cursor cursor = database.query(DatabaseHelper.UNIT_TABLE_NAME,
			    allColumns, DatabaseHelper.UNIT_ID + " = " + insertId, null,
			    null, null, null);
			
			cursor.moveToFirst();	
			cursor.close();
	   
	  }
	  
	  public void deleteUnit(Unit unit) {
		    long id = unit.getId();
		    database.delete(DatabaseHelper.UNIT_TABLE_NAME, DatabaseHelper.UNIT_ID + " = " + id, null);
	  }
	  
	  public void deleteAllUnits() {		  
		    //System.out.println("Unit deleted");
		    database.delete(DatabaseHelper.UNIT_TABLE_NAME, null, null);
	  }
	  

	  public List<Unit> getAllUnitForFacture(Integer factureID) {
		  
		  
		    List<Unit> units = new ArrayList<Unit>();
		    Cursor cursor = database.query(DatabaseHelper.UNIT_TABLE_NAME, allColumns, null, null, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	Unit unit = cursorToUnit(cursor);		    	
		    	if (unit.getFactureID().equals(factureID))
		    		units.add(unit);
		    	
		      cursor.moveToNext();
		    }
		    // make sure to close the cursor
		    cursor.close();
		    return units;
	  }
	  
	  
	  public List<Unit> getAllUnitForBill(Integer billID) {
		  
		  
		    List<Unit> units = new ArrayList<Unit>();
		    Cursor cursor = database.query(DatabaseHelper.UNIT_TABLE_NAME, allColumns, null, null, null, null, null);
		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	Unit unit = cursorToUnit(cursor);		    	
		    	if (unit.getBillID().equals(billID))
		    		units.add(unit);
		    	
		      cursor.moveToNext();
		    }		    
		    cursor.close();		
		    return units;
	  }
	  
	  public List<Unit> getAllUnits() {
		  
		  
		    List<Unit> units = new ArrayList<Unit>();
		    Cursor cursor = database.query(DatabaseHelper.UNIT_TABLE_NAME, allColumns, null, null, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	Unit unit = cursorToUnit(cursor);		    
		    	units.add(unit);		    	
		    	cursor.moveToNext();
		    }		    
		    cursor.close();		
		    return units;
	  }
	
	

	  private Unit cursorToUnit(Cursor cursor) {
		  Unit unit = new Unit();
		  unit.setId(cursor.getInt(0));
		  unit.setFactureID(cursor.getInt(1));
		  unit.setBillID(cursor.getInt(2));
		  unit.setName(cursor.getString(3));
		  unit.setMetric(cursor.getString(4));
		  unit.setPrice(cursor.getDouble(5));
		  unit.setValue(cursor.getDouble(6));
		  return unit;
	  }


}
